<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>爱家-我的房源</title>
    <link href="/static/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="/static/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet">
    <link href="/static/css/reset.css" rel="stylesheet">
    <link href="/static/plugins/bootstrap-datepicker/css/bootstrap-datepicker.min.css" rel="stylesheet">
    <link href="/static/css/ihome/main.css" rel="stylesheet">
    <link href="/static/css/ihome/myhouse.css" rel="stylesheet">
</head>

<body>
    <div class="container">
        <div class="top-bar">
            <div class="nav-bar">
                <h3 class="page-title">我的房源</h3>
                <a class="nav-btn fl" href="/my.html"><span><i class="fa fa-angle-left fa-2x"></i></span></a>
            </div>
        </div>
        <div class="houses-con">
            <ul class="houses-list auth-warn">
                <li>
                    <div class="house-title">
                        <h3>尚未进行实名认证，无法发布房屋信息！</h3>
                    </div>
                    <div class="house-content">
                        <a href="/auth.html" class="btn btn-success">去实名认证</a>
                    </div>
                </li>
            </ul>
            <ul id="houses-list" class="houses-list">
                <li>
                    <div class="new-house">
                        <a href="/newhouse.html">发布新房源</a>
                    </div>
                </li>
                <script id="my-house-tmpl" type="text/html">
                    {{each houses}}
                    <li>
                        <a href="/detail.html?id={{$value.house_id}}&f=my">
                            <div class="house-title">
                                <h3>房屋ID:{{$value.house_id}} —— {{$value.title}}</h3>
                            </div>
                            <div class="house-content">
                                <img src="{{$value.img_url}}">
                                <div class="house-text">
                                    <ul>
                                        <li>位于：{{$value.area_name}}</li>
                                        <li>价格：￥{{$value.price}}/晚</li>
                                        <li>发布时间：{{$value.ctime}}</li>
                                    </ul>
                                </div>
                            </div>
                        </a>
                    </li>
                    {{/each}}
                </script>
            </ul>
        </div>
        <div class="footer">
            <p><span><i class="fa fa-copyright"></i></span>爱家租房&nbsp;&nbsp;享受家的温馨</p>
        </div>
    </div>

    <script src="/static/js/jquery.min.js"></script>
    <script src="/static/plugins/bootstrap/js/bootstrap.min.js"></script>
    <script src="/static/plugins/bootstrap-datepicker/js/bootstrap-datepicker.min.js"></script>
    <script src="/static/plugins/bootstrap-datepicker/locales/bootstrap-datepicker.zh-CN.min.js"></script>
    <script src="/static/js/template.js"></script>
    <script>
        $(document).ready(function () {
            $.get("/api/v1/user", function (resp) {
                if (resp.errno == "0") {
                    $(".auth-warn").hide();
                    $("#houses-list").show()
                } else if (resp.errno == "4001") {
                    window.location.href = "/login.html"
                } else if (resp.errno == "4000") {
                    $(".auth-warn").show();
                    $("#houses-list").hide()
                } else {
                    alert(resp.errmsg)
                }
            }, "json");

            //获取我发布的房源
            $.get("/api/v1/houses/my", function (resp) {
                if(resp.errno == "0"){
                    var html = template('my-house-tmpl', { houses: resp.data })
                    $("#houses-list").html(`<li><div class="new-house"><a href="/newhouse.html">发布新房源</a></div></li>`);
                    $("#houses-list").append(html)
                }
            }, "json");
        })
    </script>
</body>

</html>